<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Alejandro Casas
 * Date: 1/28/13
 * Time: 7:21 PM
 * To change this template use File | Settings | File Templates.
 */
require_once 'DataLoader.php';
require "../Classes/Branch.php";
include_once('httpful-0.2.0.phar');
class InstructorBusinessWS
{
    private $client;
    public function __construct() {
        $this->client = $_SESSION['instbusiness_server_url'];
    }

    public function RegisterBusiness($name,
                                     $type,
                                     $mail,
                                     $password,$branches)
    {

        $json='';
        foreach($branches as $b)
        {
            $json=$json.'{"name":"'.$b->getName().'",
            "city":"'.$b->getCity().'",
            "country":"'.$b->getCountry().'",
            "address":"'.$b->getAddress().'",
            "lat":'.$b->getLat().',
            "lng":'.$b->getLng().',
            "openingHours":"'.$b->getOpeningHours().'",
            "mail":"'.$b->getMail().'",
            "phone":"'.$b->getPhone().'"
            },';
        }
        $json = substr($json, 0, -1);
        $uri=$this->client."RegisterBussiness";
        $parameters='{
        "name":"'.$name.'",
	    "type":"'.$type.'",
	    "mail":"'.$mail.'",
	    "password":"'.$password.'",
	    "branchlist":['.$json.']
	 }';
        $r = \Httpful\Request::post($uri)
            ->sendsJson()
            ->body($parameters)
            ->send();
        return $r;
    }

    public function RegisterInstructor($name,
                                       $birthday,
                                       $mail,
                                       $type,
                                       $gender,
                                       $password,
                                       $country,
                                       $city,
                                       $sports,
                                       $lat1,
                                       $lng1,
                                       $lat2,
                                       $lng2,
                                       $lat3,
                                       $lng3)
    {
        $slist=explode(",",$sports);
        $jsonsports='';
        foreach($slist as $s)
        {
            $jsonsports=$jsonsports.'{"name":"'.$s.'"},';
        }
        $jsonsports = substr($jsonsports, 0, -1);
        $uri=$this->client."RegisterInstructor";
        $parameters='{
        "name":"'.$name.'",
	    "birthday":"'.$birthday.'",
	    "mail":"'.$mail.'",
	    "type":"'.$type.'",
	    "gender":"'.$gender.'",
	    "password":"'.$password.'",
	    "country":"'.$country.'",
	    "city":"'.$city.'",
	    "sports":['.$jsonsports.'],
	    "lat1":'.$lat1.',
	     "lng1":'.$lng1.',
	      "lat2":'.$lat2.',
	       "lng2":'.$lng2.',
	        "lat3":'.$lat3.',
	         "lng3":'.$lng3.'
	 }';
        $r = \Httpful\Request::post($uri)
            ->sendsJson()
            ->body($parameters)
            ->send();
        return $r;
    }

    private function EditBussinessProfile()
    {
        //TODO
        return false;
    }

    private function EditInstructorProfile()
    {
        //TODO
        return false;
    }
}